#ifndef OPERATORIMPORTER_H
#define OPERATORIMPORTER_H

#define ZOOM 13//坐标和连接线长度缩放比例

#include <QObject>

#include "operatordefinition.h"
//#include "stmmodel.h"

class OperatorImporter : public QObject
{
    Q_OBJECT
public:
    explicit OperatorImporter(QObject *parent = nullptr);

    bool readOperatorFile(const QDomNode &root);

    void dataProcess_old(QFile &file,OperatorData &datas);

    void saveOperatorFile_old(const QString &redaceProjectPath,const QString &scadeProjectName,const QString &operatorFileName,OperatorData &datas);

    void saveDiagramFile(const QString &filePath,OperatorData &datas);

    void dataProcess(OperatorData &datas,QDomDocument &doc);

    void saveVariableFile(const QString &redaceProjectPath,const QString &scadeProjectName,OperatorData &datas);

    void saveOperatorFile(const QString &redaceProjectPath,const QString &scadeProjectName,const QString &operatorFileName,OperatorData &datas);

    parameter parseParameter(const QDomElement &element);

private:
    void generateOperatorFileTemplate(const QString &redaceProjectPath,const QString &scadeProjectName,const QString &operatorFileName,OperatorData &datas);

    void variableXmlAddOperator(QDomElement& Variables,QDomDocument& doc,OperatorData &datas);

    void ifblockDataXmlAddOperator(QDomElement& IfBlocks,QDomDocument& doc);

    void ifblockDataXmlAddIfnode(const IfBlock& block,const IfNode& node,QDomElement& ifnodeEle,QDomDocument& doc);

    void ifblockDataXmlAddAction(const IfBlock& block,const IfNode& node,QDomElement& actionEle,QDomDocument& doc);

    QString getIfblockFromActionOid(const QString actionOid);

    bool getIfNode(const IfBlock& block,const IfNode& node,const QString& ifnodeOid);

    void ifblockReplaceXmlAddOperator(QDomElement& IfBlocks,QDomDocument& doc);

    QDomElement ifblockReplaceXmlAddIfnode(const IfNode& node,QDomDocument& doc);

    QDomElement ifblockReplaceXmlAddAction(const BlockAction& action,QDomDocument& doc);

    void generate_ifblock_dataFiles(const QString &redaceProjectPath, const QString &scadeProjectName);

    void generate_ifblock_replaceFiles(const QString &redaceProjectPath, const QString &scadeProjectName);

    void generate_ifblock_GrammarFiles(const QString &redaceProjectPath, const QString &scadeProjectName);

    void generateStmPOUFiles();

    void variableProcess(QList<Variable> &vars,QDomDocument &doc);

    void dataStreamEquationProcess(OperatorData &datas,QDomDocument &doc);

    void stmEquationProcess(QDomDocument &doc);

    void blockEquationProcess(QDomDocument &doc);

    void equationProcess(Equation &equation,QDomDocument &doc);

    bool castImmediateProcess(QString &str);

    void emissionProcess(Equation &equation,QDomDocument &doc);

    void statemachineProcess(QDomDocument &doc);

    void ifblockProcess();

    void ifblockOidProcess();

    void ifblockInfoProcess();

    void generateStmGrammarFiles();

    void generateStateGrammarFiles();

    void WriteStateElement(QXmlStreamWriter &writer,State & state);

    void WriteTransitionElement(QXmlStreamWriter &writer,Transition & transition);

    void WriteSynchroTransitionElement(QXmlStreamWriter &writer,Transition & transition);

    void stmGEProcess();

    void transitionProcess(QDomDocument &doc);

    void stmModelOidProcess();

    QPoint findNearestPointSafe(const QRect& rect, const QPoint& point);

    QString parseStateMachine(const QDomElement &element,const QList<QDomNode>& equationGEList,const QString &parentStateOid);

    void parseIfBlock(const QDomElement &element,const QList<QDomNode>& equationGEList);

    IfNodeFork parseIfNodeFork(const QDomElement &element,const QList<QDomNode>& equationGEList);

    void parseIfNode(const QDomElement &element,const QList<QDomNode>& equationGEList);

    void parseIfBlockAction(const QDomElement &element,const QList<QDomNode>& equationGEList);

    Variable parseSignal(const QDomElement &element);

    void parseState(const QDomElement &element,StateMachine& stm,const QList<QDomNode>& equationGEList);

    void parseTransition(const QDomElement &element,State& state,const TransKind& kind);

    void correctionOperatorFile(QFile &file);

    QList<Variable> parseVariables(const QDomElement &element);

    void parseVar(const QDomElement &element);

    void parseNomalVariable(Variable &data,const QDomElement &element);

    void parseStructVariable(Variable &data,const QDomElement &element);

    void parseArrayVariable(Variable &data,const QDomElement &element);

    void parseDatas(const QDomElement &element);

    Equation parseEquation(const QDomElement &element,const QList<QDomNode>& equationGEList,const QList<Variable>& variables);

    void parseBaseOperator(const QDomElement &element,Equation &data);

    void parseUnaryOp(const QDomElement &element,Equation &data);

    void parseBinaryOp(const QDomElement &element,Equation &data);

    void parseNAryOp(const QDomElement &element,Equation &data);

    void parsePreOp(const QDomElement &element,Equation &data);

    void parseInitOp(const QDomElement &element,Equation &data);

    void parseFbyOp(const QDomElement &element,Equation &data);

    void parsePrjOp(const QDomElement &element,Equation &data);

    void parseSliceOp(const QDomElement &element,Equation &data);

    void parsePrjDynOp(const QDomElement &element,Equation &data);

    void parseDataStructOp(const QDomElement &element,Equation &data);

    void parseScalarToVectorOp(const QDomElement &element,Equation &data);

    void parseDataArrayOp(const QDomElement &element,Equation &data);

    void parseChgIthOp(const QDomElement &element,Equation &data);

    void parseIfThenElseOp(const QDomElement &element,Equation &data);

    void parseCaseOp(const QDomElement &element,Equation &data);

    void parseMakeOp(const QDomElement &element,Equation &data);

    void parseFlattenOp(const QDomElement &element,Equation &data);

    void parseNumericCastOp(const QDomElement &element,Equation &data);

    void parseTransposeOp(const QDomElement &element,Equation &data);

    void parseActivateOp(const QDomElement &element,Equation &data);

    void parseRestartOp(const QDomElement &element,Equation &data);

    void parseIteratorOp(const QDomElement &element,Equation &data);

    void parsePartialIteratorOp(const QDomElement &element,Equation &data);

    void parseOpCall(const QDomElement &element,Equation &data);

    void parsePragmas(const QDomElement &element);

    Diagram parseDiagram(const QDomElement &element,const int diaIndex);

    void diagramProcess(Diagram& diagram,int& eleId);

    void saveVariable(QXmlStreamWriter &writer,const Variable &data);

    void saveEquation(QXmlStreamWriter &writer,const Equation &data);

    void saveOperator(QXmlStreamWriter &writer,const Equation &data);

    void saveUnaryOp(QXmlStreamWriter &writer,const Equation &data);

    void saveBinaryOp(QXmlStreamWriter &writer,const Equation &data);

    void saveNAryOp(QXmlStreamWriter &writer,const Equation &data);

    void savePreOp(QXmlStreamWriter &writer,const Equation &data);

    void saveInitOp(QXmlStreamWriter &writer,const Equation &data);

    void saveFbyOp(QXmlStreamWriter &writer,const Equation &data);

    void savePrjOp(QXmlStreamWriter &writer,const Equation &data);

    void saveSliceOp(QXmlStreamWriter &writer,const Equation &data);

    void savePrjDynOp(QXmlStreamWriter &writer,const Equation &data);

    void saveDataStructOp(QXmlStreamWriter &writer,const Equation &data);

    void saveScalarToVectorOp(QXmlStreamWriter &writer,const Equation &data);

    void saveDataArrayOp(QXmlStreamWriter &writer,const Equation &data);

    void saveChgIthOp(QXmlStreamWriter &writer,const Equation &data);

    void saveIfThenElseOp(QXmlStreamWriter &writer,const Equation &data);

    void saveCaseOp(QXmlStreamWriter &writer,const Equation &data);

    void saveMakeOp(QXmlStreamWriter &writer,const Equation &data);

    void saveFlattenOp(QXmlStreamWriter &writer,const Equation &data);

    void saveIteratorOp(QXmlStreamWriter &writer,const Equation &data);

    void savePartialIteratorOp(QXmlStreamWriter &writer,const Equation &data);

    void saveOpCall(QXmlStreamWriter &writer,const Equation &data);

    void saveIdExpression(QXmlStreamWriter &writer,const QString &data);

    void saveParameter(QXmlStreamWriter &writer,const parameter &data);

    void saveEquationGE(QXmlStreamWriter &writer,const QList<Equation>& equations,int &eleIDindex,int &outPortIndex);

    void saveLineGE(QXmlStreamWriter &writer,const Diagram& diagram,int &eleIDindex);

    void parseEquationPorts(Equation &data);

    void parseUnaryOpPorts(Equation &data);

    void parseBinaryOpPorts(Equation &data);

    void parseNAryOpPorts(Equation &data);

    void parsePreOpPorts(Equation &data);

    void parseInitOpPorts(Equation &data);

    void parseFbyOpPorts(Equation &data);

    void parsePrjOpPorts(Equation &data);

    void parseSliceOpPorts(Equation &data);

    void parsePrjDynOpPorts(Equation &data);

    void parseDataStructOpPorts(Equation &data);

    void parseScalarToVectorOpPorts(Equation &data);

    void parseDataArrayOpPorts(Equation &data);

    void parseChgIthOpPorts(Equation &data);

    void parseIfThenElseOpPorts(Equation &data);

    void parseCaseOpPorts(Equation &data);

    void parseMakeOpPorts(Equation &data);

    void parseFlattenOpPorts(Equation &data);

    void parseNumericCastOpPorts(Equation &data);

    void parseTimesOpPorts(Equation &data);

    void parseLslOpPorts(Equation &data);

    void parseLsrOpPorts(Equation &data);

    void parseTransposeOpPorts(Equation &data);

    void parseActivateOpPorts(Equation &data);

    void parseRestartOpPorts(Equation &data);

    void parseIteratorOpPorts(Equation &data);

    void parsePartialIteratorOpPorts(Equation &data);

    void parseOpCallPorts(Equation &data);

    QString parseIdExpression(const QDomElement &element);   

    QString getOid(const QDomNode &idNode);

    QString getComment(const QDomNode &commentNode);

    bool removeXmlComments(const QString& filePath);

    void removeCommentsRecursive(QDomNode& node);

    void findAllElementsByName(const QDomElement &element, const QString &targetName, QList<QDomElement> &results);

public:
    OperatorData m_operator;
    QHash<QString,QString> m_hash;//数据类型映射表
    //StmModel m_stmModel;

};

#endif // OPERATORIMPORTER_H
